/*
 * @lc app=leetcode.cn id=530 lang=csharp
 *
 * [530] 二叉搜索树的最小绝对差
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    private int minDiff = int.MaxValue;
    private TreeNode prev = null;
    
    public int GetMinimumDifference(TreeNode root) {
        InOrderTraversal(root);
        return minDiff;
    }
    
    private void InOrderTraversal(TreeNode node) {
        if (node == null) return;
        
        // 遍历左子树
        InOrderTraversal(node.left);
        
        // 处理当前节点
        if (prev != null) {
            minDiff = Math.Min(minDiff, node.val - prev.val);
        }
        prev = node;
        
        // 遍历右子树
        InOrderTraversal(node.right);
    }
}
// @lc code=end

